package com.chenfan.filestore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenfan.filestore.dto.BatchImportTemplateQueryDTO;
import com.chenfan.filestore.model.BatchExportTemplate;
import com.chenfan.filestore.model.BatchImportTemplate;
import com.chenfan.filestore.repository.BatchImportTemplateMapper;
import com.chenfan.filestore.service.BatchImportTemplateService;
import com.chenfan.filestore.utils.LambdaQueryUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author K
* @date 2023/5/16 10:48 AM
*
*/
@Service
public class BatchImportTemplateServiceImpl
        extends ServiceImpl<BatchImportTemplateMapper, BatchImportTemplate> implements BatchImportTemplateService {
    @Override
    public Page<BatchImportTemplate> pageBatchImportTemplate(BatchImportTemplateQueryDTO query) {
        Page<BatchImportTemplate> page = new Page<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<BatchImportTemplate> lambdaQueryWrapper = LambdaQueryUtil.newLambdaQuery();
        lambdaQueryWrapper.eq(Objects.nonNull(query.getSystemId()), BatchImportTemplate::getSystemId, query.getSystemId());
        lambdaQueryWrapper.eq(Objects.nonNull(query.getSystemId()), BatchImportTemplate::getSystemId, query.getSystemId());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(query.getTemplateCode()), BatchImportTemplate::getTemplateCode, query.getTemplateCode());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(query.getTemplateName()), BatchImportTemplate::getTemplateName, query.getTemplateName());
        lambdaQueryWrapper.eq(Objects.nonNull(query.getStatus()), BatchImportTemplate::getStatus, query.getStatus());
        lambdaQueryWrapper.ge(Objects.nonNull(query.getUpdateDateStart()), BatchImportTemplate::getUpdateDate, query.getUpdateDateStart());
        lambdaQueryWrapper.le(Objects.nonNull(query.getUpdateDateEnd()), BatchImportTemplate::getUpdateDate, query.getUpdateDateEnd());
        lambdaQueryWrapper.orderByDesc(BatchImportTemplate::getUpdateDate);
        return this.page(page, lambdaQueryWrapper);
    }

    @Override
    public BatchImportTemplate findByTemplateCode(String templateCode) {
        LambdaQueryWrapper<BatchImportTemplate> lambdaQueryWrapper = LambdaQueryUtil.newLambdaQuery();
        lambdaQueryWrapper.eq(BatchImportTemplate::getTemplateCode, templateCode);
        return this.getOne(lambdaQueryWrapper);
    }
}
